/**
 * AiPrompts 页面专用JavaScript功能
 * 包含搜索、分类、弹窗等特殊交互功能
 */

// 移动端菜单切换
function initMobileMenu() {
    const mobileMenuButton = document.getElementById('mobile-menu-button');
    const mobileMenu = document.getElementById('mobile-menu');
    
    if (mobileMenuButton && mobileMenu) {
        mobileMenuButton.addEventListener('click', () => {
            mobileMenu.classList.toggle('hidden');
        });
    }
}

// 导航栏滚动效果
function initNavbarScroll() {
    const navbar = document.getElementById('navbar');
    
    if (navbar) {
        window.addEventListener('scroll', () => {
            if (window.scrollY > 10) {
                navbar.classList.add('shadow-md');
                navbar.classList.remove('shadow-sm');
            } else {
                navbar.classList.remove('shadow-md');
                navbar.classList.add('shadow-sm');
            }
        });
    }
}

// 分类标签点击效果
function initCategoryTags() {
    const tags = document.querySelectorAll('#category-tags button');
    
    tags.forEach(tag => {
        tag.addEventListener('click', () => {
            // 移除所有标签的激活状态
            tags.forEach(t => {
                t.classList.remove('tag-active');
                t.classList.add('bg-white', 'border', 'border-neutral-200', 'text-neutral-600');
            });
            
            // 添加当前标签的激活状态
            tag.classList.add('tag-active');
            tag.classList.remove('bg-white', 'border', 'border-neutral-200', 'text-neutral-600');
            
            // 执行搜索（结合分类和关键词）
            performSearch();
        });
    });
}

// 卡片交互效果
function initCardInteractions() {
    const cards = document.querySelectorAll('.card-hover');
    
    cards.forEach(card => {
        card.addEventListener('mouseenter', () => {
            card.style.transform = 'translateY(-5px)';
            card.style.boxShadow = '0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04)';
        });
        
        card.addEventListener('mouseleave', () => {
            card.style.transform = 'translateY(0)';
            card.style.boxShadow = 'none';
        });
    });
}

// 搜索功能
function initSearchFunction() {
    const categorySearchInput = document.getElementById('category-search');
    const promptList = document.getElementById('prompt-list');
    const emptyState = document.getElementById('empty-state');
    const categoryTags = document.getElementById('category-tags');
    
    // 分类搜索功能
    if (categorySearchInput) {
        categorySearchInput.addEventListener('input', filterCategories);
    }
    
    function filterCategories() {
        const searchTerm = categorySearchInput.value.toLowerCase().trim();
        const categoryButtons = categoryTags.querySelectorAll('button');
        
        categoryButtons.forEach(button => {
            const categoryText = button.textContent.toLowerCase();
            if (searchTerm === '' || categoryText.includes(searchTerm)) {
                button.classList.remove('hidden');
            } else {
                button.classList.add('hidden');
            }
        });
        
        // 如果所有标签都被隐藏，显示空状态
        const visibleTags = categoryTags.querySelectorAll('button:not(.hidden)');
        if (visibleTags.length === 0) {
            categoryTags.classList.add('hidden');
            // 这里可以添加空状态提示
        } else {
            categoryTags.classList.remove('hidden');
        }
    }
    
    // 执行搜索
    window.performSearch = function() {
        const searchTerm = categorySearchInput.value.toLowerCase().trim();
        const activeTag = document.querySelector('.tag-active');
        const activeCategory = activeTag ? activeTag.dataset.category.toLowerCase() : '';
        
        // 这里可以添加实际的搜索逻辑
        console.log('执行搜索:', { searchTerm, activeCategory });
    };
}

// 弹窗功能
function initModalFunction() {
    const modal = document.getElementById('prompt-modal');
    const modalClose = document.getElementById('prompt-modal-close');
    const modalCopyBtn = document.getElementById('modal-copy-btn');
    const modalPrompt = document.getElementById('modal-prompt');
    
    // 关闭弹窗
    if (modalClose) {
        modalClose.addEventListener('click', () => {
            modal.classList.add('hidden');
        });
    }
    
    // 点击遮罩关闭
    if (modal) {
        modal.addEventListener('click', (e) => {
            if (e.target === modal) {
                modal.classList.add('hidden');
            }
        });
    }
    
    // 复制按钮功能
    if (modalCopyBtn && modalPrompt) {
        modalCopyBtn.addEventListener('click', () => {
            const text = modalPrompt.innerText;
            navigator.clipboard.writeText(text).then(() => {
                const icon = modalCopyBtn.querySelector('i');
                const oldClass = icon.className;
                icon.className = 'fa fa-check text-green-500';
                setTimeout(() => { icon.className = oldClass; }, 1200);
            });
        });
    }
}

// 卡片点击事件
function initCardClickEvents() {
    const promptList = document.getElementById('prompt-list');
    
    if (promptList) {
        promptList.addEventListener('click', function(e) {
            let card = e.target;
            while (card && !card.classList.contains('card-hover') && card !== promptList) {
                card = card.parentElement;
            }
            if (card && card.classList && card.classList.contains('card-hover')) {
                const idx = card.getAttribute('data-idx');
                // 这里可以添加实际的卡片点击逻辑
                console.log('点击卡片:', idx);
            }
        });
    }
}

// 初始化所有AiPrompts页面功能
function initPromptsFeatures() {
    initMobileMenu();
    initNavbarScroll();
    initCategoryTags();
    initCardInteractions();
    initSearchFunction();
    initModalFunction();
    initCardClickEvents();
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', initPromptsFeatures); 